﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BT_Longnv.Models;

namespace BT_Longnv.Controllers
{
    public class BTVNController : Controller
    {
        //
        // GET: /BTVN/

        public ActionResult HomeIndex()
        {
            var db = new BTVNContext();
            var listProduct = db.TableProduct.Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate
            });
            return View(listProduct);
        }
        public ActionResult MenuPartial()
        {
            var db = new BTVNContext();
            var listCategory = db.TableProduct.GroupBy(item => item.CategoryId).Select(item => new ProductModel
            {
                ProductId = item.FirstOrDefault().ProductId,
                ProductName = item.FirstOrDefault().ProductName,
                CategoryName = item.FirstOrDefault().Category.CategoryName,
                CategoryId = item.FirstOrDefault().CategoryId
            }).Distinct();
            return PartialView("_MenuPartial", listCategory);
        }

        public ActionResult ListProduct()
        {
            var db = new BTVNContext();
            int ctg = Convert.ToInt32(Request.Params["ctg"]);
            var listProduct = db.TableProduct.Where(item => item.CategoryId == ctg).Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate
            });
            return View(listProduct);
        }

        public ActionResult NewProduct()
        {
            var db = new BTVNContext();
            int ctg = Convert.ToInt32(Request.Params["ctg"]);
            var listProduct = db.TableProduct.Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate
            }).OrderByDescending(item => item.ImportDate).Take(5);
            return View(listProduct);
        }
        public ActionResult HotProduct()
        {
            var db = new BTVNContext();
            int ctg = Convert.ToInt32(Request.Params["ctg"]);
            var listProduct = db.TableProduct.Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate
            }).OrderByDescending(item => item.UnitInOrder).Take(5);
            return View(listProduct);
        }

        public ActionResult OderProduct()
        {
            int ctg = Convert.ToInt32(Request.Params["ctg"]);
            var db = new BTVNContext();
            var listOrderProduct = db.TableProduct.Where(item => item.CategoryId == ctg).Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                CategoryName = item.Category.CategoryName,
                CategoryId = item.CategoryId
            }).Distinct();
            var listOther = db.TableProduct.Where(item => item.CategoryId != ctg).Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate
            }).Distinct();
            ViewBag.Other = listOther;
            return View(listOrderProduct);
        }
        public ActionResult OderDetail()
        {
            var db = new BTVNContext();
            int pro = Convert.ToInt32(Request.Params["pro"]);
            var listProduct = db.TableProduct.Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
                UnitPrice = item.UnitPrice,
                Piture = item.Piture,
                UnitInOrder = item.UnitInOrder,
                ImportDate = item.ImportDate,
                CategoryName= item.Category.CategoryName
            }).Distinct();
            ViewBag.listProduct = listProduct;
            ViewBag.name = listProduct.FirstOrDefault().CategoryName;

            return View(db.TableProduct.Select(item => new ProductModel
            {
                ProductId = item.ProductId,
                ProductName = item.ProductName,
            }).Where(item => item.ProductId == pro).FirstOrDefault());


        }


    }
}
